//
//  YZMyStocksCell.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/30.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMyStocksCell.h"
#import "YZMAppStyle.h"
#import "YZMStockInfo.h"
#import "YZMStockRealModel.h"
#import "NSNumber+Utils.h"
#import "UILabel+Color.h"
#import <Masonry/Masonry.h>

@interface YZMyStocksCell ()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *codeLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *rangeLabel;
@property (nonatomic, strong) UILabel *rankLB;

@end


@implementation YZMyStocksCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle: style reuseIdentifier: reuseIdentifier];
    if (self) {
        self.selectionStyle  = UITableViewCellSelectionStyleNone;
        [self createObjects];
        self.rightMargin = 10.0;
        self.leftMargin = 10.0;
    }
    return self;
}

- (void)setRecord:(YZMStockInfo *)record{
    
    _record = record;
    self.nameLabel.text = record.name;
    self.codeLabel.text = record.code;
    self.priceLabel.text = @"--";
    self.rangeLabel.text = @"--";
    self.rangeLabel.textColor = [YZMAppStyle colorForLightGrayText];
    self.rankLB.text = [record.code substringWithRange:NSMakeRange(0, 2)];
}

-(void)setRealRecord:(YZMStockRealModel *)realRecord{
    
    _realRecord = realRecord;
    
    if ([@(realRecord.price) isEqualWithDouble:0.0]) {
        
        self.priceLabel.text = @(realRecord.lastClosePrice).p02fString;
        self.rangeLabel.text = @"停牌";
        self.rangeLabel.textColor = [YZMAppStyle colorForLightGrayText];
    }else{
        
        self.priceLabel.text = @(realRecord.price).p02fString;
        if ([@(realRecord.price) isGreaterThanDouble:realRecord.lastClosePrice]) {
            
            self.rangeLabel.textColor = [YZMAppStyle colorForRiseText];
            self.rangeLabel.text = [NSString stringWithFormat:@"+%@%%",@(realRecord.price/realRecord.lastClosePrice*100-100).p02fString];
            
        }else if([@(realRecord.price) isEqualWithDouble:realRecord.lastClosePrice]){
            
            self.rangeLabel.textColor = [YZMAppStyle colorForLightGrayText];
            self.rangeLabel.text = [@(realRecord.price/realRecord.lastClosePrice*100-100).p02fString stringByAppendingString:@"%"];
            
        }else{
            
            self.rangeLabel.textColor = [YZMAppStyle colorForDropText];
            self.rangeLabel.text = [@(realRecord.price/realRecord.lastClosePrice*100-100).p02fString stringByAppendingString:@"%"];
        }
    }
    
    self.rankLB.text = [realRecord.code substringWithRange:NSMakeRange(0, 2)];
}

- (void)createObjects{
    
    self.rankLB = [YZMAppStyle labelWithRedBGAndWhiteFont];
    self.rankLB.font = [YZMAppStyle fontWithSize:9];
    self.rankLB.backgroundColor = [YZMAppStyle colorForRedMark];
    [self.contentView addSubview:self.rankLB];
    [self.rankLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(20);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(12);
        make.top.equalTo(self.contentView).offset(15);
    }];
    
    self.nameLabel = [YZMAppStyle labelWithLargeWhite];
    [self.contentView addSubview: self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rankLB.mas_right).offset(6);
        make.bottom.equalTo(self.contentView.mas_centerY).offset(4);
    }];
    
    self.codeLabel  =[YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:10.0] andColor:[YZMAppStyle colorForLightGrayText]];
    [self.contentView addSubview: self.codeLabel];
    [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.contentView.mas_centerY).offset(6);
    }];
    
    self.priceLabel = [YZMAppStyle labelWithMediumWhite];
    [self.contentView addSubview: self.priceLabel];
    self.priceLabel.text  = @"--";
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_centerX).offset(40);
        make.centerY.equalTo(self.contentView);
    }];
    
    self.rangeLabel = [YZMAppStyle labelWithFont:[YZMAppStyle mediumSizeFont] andColor:[YZMAppStyle colorForRiseText]];
    [self.contentView addSubview:self.rangeLabel];
    self.rangeLabel.text = @"--";
    [self.rangeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.contentView).offset(-20);
        make.centerY.equalTo(self.contentView);
    }];
}

@end
